#ifndef ESPMONITOR_H
#define ESPMONITOR_H

#include <QtCore/qglobal.h>
#if QT_VERSION >= 0x050000
#include <QtWidgets/QMainWindow>
#else
#include <QtGui/QMainWindow>
#endif

#include <QPointer>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTimer>

QT_BEGIN_NAMESPACE
namespace Ui { class ESPMonitor; }
QT_END_NAMESPACE

class QLabel;

class ESPMonitor : public QMainWindow
{
    Q_OBJECT

public:
    explicit ESPMonitor(QWidget *parent = nullptr);
    ~ESPMonitor();

    /**
     * @brief 开始监视器
     */
    void start();

    /**
     * @brief 结束监视器
     */
    void stop();

    /**
     * @brief 检测指定名称的进程是否运行
     * @param name
     * @return
     */
    bool detectionProcess();

    QString getTimestamp();

public slots:

    /**
     * @brief 在状态栏显示一条消息
     * @param text
     * @param timeout
     */
    void showStatusBarMessage(const QString &text, int timeout = 0);

    /**
     * @brief 显示一个消息框
     * @param text
     * @param timeout
     */
    void showMessageBox(const QString &title, const QString &text);

    void updateSerialPortList();

    void updateSerialPortStatus();

    void updateSerialDataCount();

    /**
     * @brief 向串口写数据
     * @param data
     */
    bool serialWrite(const QByteArray & data);

    /**
     * @brief 向串口读数据
     * @return
     */
    void serialRead();

    void historyAppendWrite(const QString & data);
    void historyAppendRead(const QString & data);

    void startIdfDetect();
    void stopIdfDetect();

private:
    Ui::ESPMonitor *ui;
    QPointer<QSerialPort> m_serial; //!< 通讯串口
    QList<QSerialPortInfo> m_serialInfos; //!< 串口设备信息
    bool m_started = false; //!< 串口通讯是否开始
    bool m_suspend = false; //!< 串口通讯是否暂停
    int64_t m_readCount = 0; //!< 串口接收计数 (字节数)
    int64_t m_writeCount = 0; //!< 串口发送计数
    int32_t m_checkTimerId = 0; //!< 检测进程的定时器
    QTimer m_repartTimer; //!< 重复发送定时器
    QTimer m_readCheckTimer; //!< 读取检查定时器

    //串口状态控件
    QPointer<QWidget> m_readHit; //!< 数据接收状态
    QPointer<QWidget> m_writeHit; //!< 数据发送状态
    QPointer<QLabel> m_readCountLab; //!< 数据接收计数
    QPointer<QLabel> m_writeCountLab; //!< 数据发送计数
    QPointer<QLabel> m_serialPortStatusLab; //!< 串口状态

    // QObject interface
protected:
    virtual void timerEvent(QTimerEvent *event) override;

protected:
    Q_PROPERTY(bool started READ started WRITE setStarted NOTIFY startedChanged)
public:
    inline bool started() const { return m_started; }
public slots:
    void setStarted(const bool &  started );
signals:
    void startedChanged(const bool & started);

protected:
    Q_PROPERTY(bool suspend READ suspend WRITE setSuspend NOTIFY suspendChanged)
public:
    inline bool suspend() const { return m_suspend; }
public slots:
    void setSuspend(const bool &  suspend );
signals:
    void suspendChanged(const bool & suspend);

protected:
    Q_PROPERTY(int64_t readCount READ readCount WRITE setReadCount NOTIFY readCountChanged)
public:
    int64_t readCount() const { return m_readCount; }
public slots:
    void setReadCount(const int64_t &  readCount );
signals:
    void readCountChanged(const int64_t & readCount);

protected:
    Q_PROPERTY(int64_t writeCount READ writeCount WRITE setWriteCount NOTIFY writeCountChanged)
public:
    int64_t writeCount() const { return m_writeCount; }
public slots:
    void setWriteCount(const int64_t &  writeCount );
signals:
    void writeCountChanged(const int64_t & writeCount);

public:
    virtual void closeEvent(QCloseEvent *event) override;
};


#endif // ESPMONITOR_H
